package com.hly.tool.notice;

import com.dassault_systemes.i3dx.appsservices.notifications.nomatrix.NotifConf;
import com.dassault_systemes.i3dx.appsservices.notifications.nomatrix.NotificationBasicUtil;
import com.matrixone.apps.domain.util.MqlUtil;
import matrix.db.Context;

/**
 * @author ：Jack Hao
 * @date ：Created in 2021/9/9 10:42
 * @description：
 */
public class Notice
{
    private  static  NotifConf notifConf = null;


    /**
     * 发送消息通知
     * @param context
     * @param msg
     * @throws Exception
     */
    public static void sendNotif(Context context, String msg) throws Exception
    {
        try
        {
            String notificationUrl = get3DNotificationsURL() + "/api/notify";
            notifConf = NotifConf.getInstance();
            notifConf.init(notificationUrl, 10000);
            NotificationBasicUtil.sendNotification(notifConf, msg);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }


    /**
     * 获取要通知APP的地址
     *
     * @return
     * @throws Exception
     */
    private static String get3DNotificationsURL() throws Exception
    {
        String notificationUrl = System.getenv("NOTIFICATION_URL");
        if (notificationUrl == null || notificationUrl.isEmpty())
        {
            String myappsUrl = System.getenv("MYAPPS_URL");
            if (myappsUrl == null || "".equals(myappsUrl))
            {
                myappsUrl = "http://localhost";
            }
            myappsUrl = myappsUrl.replace("apps", "intf");
            myappsUrl = myappsUrl.replace("enovia", "");
            if (myappsUrl.contains(":443/"))
            {
                myappsUrl = myappsUrl.split(":443/")[0];
            }
            notificationUrl = myappsUrl;
        }
        return notificationUrl;
    }



    /**
     * This method displays mql error message.
     *
     * @param context the eMatrix <code>Context</code> object
     * @param error String contains error message
     * @throws Exception if the operation fails
     * @since AEF Rossini
     */

    public static void mqlError(Context context, String error)
            throws Exception
    {
        MqlUtil.mqlCommand(context, "error $1",error);

    }

    /**
     * This method displays mql notice message.
     *
     * @param context the eMatrix <code>Context</code> object
     * @param notice String containing notice message
     * @throws Exception if the operation fails
     * @since AEF Rossini
     */

    public static void mqlNotice(Context context, String notice)
            throws Exception
    {
        MqlUtil.mqlCommand(context, "notice $1", notice);
    }

    /**
     * This method displays mql warning message.
     *
     * @param context the eMatrix <code>Context</code> object
     * @param warning String containing warning message
     * @throws Exception if the operation fails
     * @since AEF Rossini
     */
    public static void mqlWarning(Context context, String warning)
            throws Exception
    {
        MqlUtil.mqlCommand(context, "warning $1", warning);
    }
}
